package com.t.bricks.bussiness.db.dao.useranddepartment.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.useranddepartment.UserAndDepartmentDao;
import com.t.bricks.bussiness.db.entity.useranddepartment.UserAndDepartment;
import com.t.bricks.bussiness.db.mapper.useranddepartment.UserAndDepartmentMapper;
import com.t.bricks.bussiness.model.useranddepartment.LoginUser;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class UserAndDepartmentDaoImpl implements UserAndDepartmentDao {
	
	@Autowired
	private UserAndDepartmentMapper UserAndDepartmentMapper;

	/**
	 * 根据关键值查数量
	 * @param UserAndDepartmentData 存放查询条件的UserAndDepartment对象
	 * @return
	 */
	@Override
	public MsgEmity findCountByKey(UserAndDepartment UserAndDepartmentData) {
		int iCount = 0;
		try {
			iCount = UserAndDepartmentMapper.findCountByKey(UserAndDepartmentData);
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7001);
		}
		
		return new MsgEmity(true, "查询成功！", iCount);
	}

	/**
	 * 根据人员编号取关联的部门信息
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity findBysUserId(String sUserId) {
		List<UserAndDepartment> list = null;
		try {
			list = UserAndDepartmentMapper.findBysUserId(sUserId);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}
	
	/**
	 * 批量保存人员所属部门关联信息
	 * 注意:若部门信息为空则会清除所有该人员与部门的关联信息
	 * @param sUserId 人员编号
	 * @param list 部门与人员关系对象集合
	 * @return
	 */
	@Transactional
	@Override
	public MsgEmity save(String sUserId, List<UserAndDepartment> list) {
		int iCode = 7002;
		try {
			UserAndDepartmentMapper.del(sUserId);//注意:启用了事务,先删除,然后再添加
			
			if (null == list || list.size() < 1) {
				return new MsgEmity(true, "解除关联成功！", 7999);
			}
			
			int i = UserAndDepartmentMapper.save(list);
			if (i < 1) {
				Log.error("关联未能得到预期影响值(预期大于等于1):", i);
				iCode = 7001;
				throw new RuntimeException("关联失败！");
			}
		} catch (Exception e) {
			Log.error("关联时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return new MsgEmity(false, "关联失败！", iCode);
		}
		
		return new MsgEmity(true, "关联成功！", 7999);
	}
	
	/**
	 * 清除所有该人员与部门的关联信息
	 * @param sUserId 人员编号
	 * @return
	 */
	@Override
	public MsgEmity del(String sUserId) {
		try {
			int i = UserAndDepartmentMapper.del(sUserId);
			if (i > 0) {
				return new MsgEmity(true, "删除成功！", 7999);
			}
			
			i = UserAndDepartmentMapper.findCountByUser(sUserId);
			if (i > 0) {
				return new MsgEmity(false, "删除失败！", 7101);
			}
			
			return new MsgEmity(true, "删除成功，已经没有关联的部门！", 7999);
		} catch (Exception e) {
			Log.error("删除失败：", e.getMessage());
			return new MsgEmity(false, "删除失败，操作数据库异常！", 7102);
		}
	}
	
	/**
	 * 根据部门编号查询员工
	 * @param sDeparmentId 部门编号
	 * @param iMainDeparment 仅限主部门
	 * @return
	 */
	@Override
	public MsgEmity findByDepartmentId(String sDeparmentId, Integer iMainDeparment) {
		List<LoginUser> list = null;
		try {
			list = UserAndDepartmentMapper.findByDepartmentId(sDeparmentId, iMainDeparment);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

}
